package com.example.test630;

import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;

import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.TextView;

import java.io.IOException;

import okhttp3.Call;
import okhttp3.Callback;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.Response;

public class MainActivity extends AppCompatActivity {
    private TextView infoTv;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        infoTv = findViewById(R.id.info_text);
    }

    public void getInfo(View view) {
        Intent intent = new Intent(this,OtherActivity.class);
        startActivityForResult(intent,1);
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {
        if (requestCode == 1 && resultCode == 1) {
            String info = data.getStringExtra("info");
            infoTv.setText(info);
        }
    }

    public void getJson(View view) {
        final OkHttpClient client = new OkHttpClient();
        final Request request = new Request.Builder().url("https://api.myjson.com/bins/84z5v")
                .get().build();
        client.newCall(request).enqueue(new Callback() {
            @Override
            public void onFailure(Call call, IOException e) {

            }

            @Override
            public void onResponse(Call call, Response response) throws IOException {
                String sr = response.body().string();
                Log.i("TAG...", "onResponse: " + sr);
            }
        });

//        new Thread() {
//            @Override
//            public void run() {
//                try{
//                    Response response = client.newCall(request).execute();
//                    String s = response.body().string();
//                    Log.i("response is :","run:" +s);
//                }catch (IOException e) {
//                    e.printStackTrace();
//                }
//            }
//        }.start();

    }
}
